package com.morizhang.merchanmanager.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.morizhang.merchanmanager.api.ApiResult;
import com.morizhang.merchanmanager.api.IService;
import com.morizhang.merchanmanager.api.model.ShopShelfModel;
import com.morizhang.merchanmanager.api.plugins.BasicWareManage;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Method;
import java.util.List;

@ServiceAnnotation(name = "ShopShelfManageService")
public class ShopShelfManageService implements IService {
    @Autowired
    private BasicWareManage basicWareManage;
    @Override
    public ApiResult callService(JSONObject requestParams) throws Exception {
        String callMethodName = requestParams.getString("requestMethod");
        Method callMethod = this.getClass().getMethod(callMethodName, JSONObject.class);
        ApiResult result = (ApiResult)callMethod.invoke(this, requestParams);
        result.setSuccess(true);
        return result;
    }

    public ApiResult selectAllShop(JSONObject requestParams){
        List<ShopShelfModel> resultList = basicWareManage.selectAllShop();
        JSONArray resultJsonArray = JSON.parseArray(JSON.toJSONString(resultList));
        ApiResult result = new ApiResult();
        result.setData(resultJsonArray);
        return result;
    }

    public ApiResult queryShopInfo(JSONObject requestParams){
        String shopName  = requestParams.getJSONObject("data").getString("shopName");
        ShopShelfModel resultModel = basicWareManage.queryShopInfo(shopName);
        JSONObject resultJsonObject = JSON.parseObject(JSON.toJSONString(resultModel));
        ApiResult result = new ApiResult();
        result.setData(resultJsonObject);
        return result;
    }

    public ApiResult addShopInfo(JSONObject requestParams) throws Exception {
        JSONObject shopInfo = requestParams.getJSONObject("data");
        ShopShelfModel shopModel = JSON.toJavaObject(shopInfo, ShopShelfModel.class);
        basicWareManage.addShopInfo(shopModel);
        return new ApiResult();
    }

    public ApiResult deleteShopInfo(JSONObject requestParams){
        String shopName  = requestParams.getJSONObject("data").getString("shopName");
        basicWareManage.deleteShopInfo(shopName);
        return new ApiResult();
    }

    public ApiResult updateShopInfo(JSONObject requestParams){
        JSONObject shopInfo  = requestParams.getJSONObject("data");
        ShopShelfModel shopModel = JSON.toJavaObject(shopInfo, ShopShelfModel.class);
        basicWareManage.updateShopInfo(shopModel);
        return new ApiResult();
    }

    public ApiResult selectAllShopSection(JSONObject requestParams){
        String shopNo  = requestParams.getJSONObject("data").getString("shopNo");
        List<ShopShelfModel> resultList = basicWareManage.selectAllShopSection(shopNo);
        JSONArray resultJsonArray = JSON.parseArray(JSON.toJSONString(resultList));
        ApiResult result = new ApiResult();
        result.setData(resultJsonArray);
        return result;
    }

    public ApiResult queryShopSectionInfo(JSONObject requestParams){
        String shopSectionNo  = requestParams.getJSONObject("data").getString("shopSectionNo");
        ShopShelfModel resultModel = basicWareManage.queryShopSectionInfo(shopSectionNo);
        JSONObject resultJsonObject = JSON.parseObject(JSON.toJSONString(resultModel));
        ApiResult result = new ApiResult();
        result.setData(resultJsonObject);
        return result;
    }

    public ApiResult addShopSectionInfo(JSONObject requestParams) throws Exception {
        JSONObject shopSectionInfo = requestParams.getJSONObject("data");
        ShopShelfModel shopModel = JSON.toJavaObject(shopSectionInfo, ShopShelfModel.class);
        basicWareManage.addShopSectionInfo(shopModel);
        return new ApiResult();
    }

    public ApiResult deleteShopSectionInfo(JSONObject requestParams){
        String shopSectionName  = requestParams.getJSONObject("data").getString("shopSectionName");
        basicWareManage.deleteShopSectionInfo(shopSectionName);
        return new ApiResult();
    }

    public ApiResult updateShopSectionInfo(JSONObject requestParams){
        JSONObject shopInfo  = requestParams.getJSONObject("data");
        ShopShelfModel shopSectionModel = JSON.toJavaObject(shopInfo, ShopShelfModel.class);
        basicWareManage.updateShopSectionInfo(shopSectionModel);
        return new ApiResult();
    }

    public ApiResult selectAllShopShelf(JSONObject requestParams){
        String shopSectionNo  = requestParams.getJSONObject("data").getString("shopSectionNo");
        List<ShopShelfModel> resultList = basicWareManage.selectAllShopShelf(shopSectionNo);
        JSONArray resultJsonArray = JSON.parseArray(JSON.toJSONString(resultList));
        ApiResult result = new ApiResult();
        result.setData(resultJsonArray);
        return result;
    }

    public ApiResult queryShopShelfInfo(JSONObject requestParams){
        String shopShelfNo  = requestParams.getJSONObject("data").getString("shopShelfNo");
        ShopShelfModel resultModel = basicWareManage.queryShopShelfInfo(shopShelfNo);
        JSONObject resultJsonObject = JSON.parseObject(JSON.toJSONString(resultModel));
        ApiResult result = new ApiResult();
        result.setData(resultJsonObject);
        return result;
    }

    public ApiResult addShopShelfInfo(JSONObject requestParams) throws Exception {
        JSONObject shopShelfInfo = requestParams.getJSONObject("data");
        ShopShelfModel shopModel = JSON.toJavaObject(shopShelfInfo, ShopShelfModel.class);
        basicWareManage.addShopShelfInfo(shopModel);
        return new ApiResult();
    }

    public ApiResult deleteShopShelfInfo(JSONObject requestParams){
        String shopShelfName  = requestParams.getJSONObject("data").getString("shopShelfName");
        basicWareManage.deleteShopShelfInfo(shopShelfName);
        return new ApiResult();
    }

    public ApiResult updateShopShelfInfo(JSONObject requestParams){
        JSONObject shopShelfInfo  = requestParams.getJSONObject("data");
        ShopShelfModel shopSectionModel = JSON.toJavaObject(shopShelfInfo, ShopShelfModel.class);
        basicWareManage.updateShopShelfInfo(shopSectionModel);
        return new ApiResult();
    }
}
